<?php

require_once 'constants.php';
require_once 'Module_File.php';
require_once 'Module_Acl.php';
require_once 'Module_Ajax_Action.php';

class Module{
    private $CI;   
    private $controller;
    private $modele;
    
    private $status = MODULE_INACTIVE;
    
    private $acl = array();
    private $session = array();
    private $ajax_actions = array();
    
    private $view_path = "index";
    
    
    public static function get($name){
        $db = get_instance()->db;
        $query = $db->where(array('name' => $name))->get('module');
        foreach ($query->result() as $result)
        {
            return new Module($result);
        }
    }
    
    public function __construct($db_module) {
        $this->modele = $db_module;
        $this->CI = & get_instance();
    }
    
    public function setController($c) {
        $this->controller = $c;
    }
    
    public function getController() {
        return $this->controller;
    }

    public function addSessionData($name, $data) {
        $this->session[$name] = $data;
    }

    public function getSessionData($name) {
        return $this->session[$name];
    }

    public function addCSSFile($name) {
        $this->css_files[count($this->css_files)] = $this->modele->css_dir . '/' . $name;
    }

    public function getCSSDirectory() {
        return $this->modele->css_dir;
    }

    public function getPHPDirectory() {
        return $this->modele->php_dir;
    }

    public function img_url($path) {
        return MODULE_REPOSITORY . '/' . $this->modele->name . '/' . $this->modele->img_dir . '/' . $path;
    }

    public function getStatus() {
        return $this->status;
    }

    public function getCategory() {
        return $this->modele->category;
    }

    public function is_installed() {
        $db = $this->CI->db;

        $db->query("CREATE TABLE IF NOT EXISTS module(
                id int(11) NOT NULL AUTO_INCREMENT,
                
                name VARCHAR(100) NOT NULL UNIQUE,
                display_name VARCHAR(100) NOT NULL,
                category VARCHAR(100),
                
                php_dir VARCHAR(255) NOT NULL,
                css_dir VARCHAR(255),
                js_dir VARCHAR(255),
                img_dir VARCHAR(255),
                
                allow TEXT,
                deny TEXT,
                
                ajax_mode int(1),
                
                PRIMARY KEY (id));");

        $db->where('name', $this->modele->name);
        $query = $db->get('module');
        if ($query->num_rows() > 0) {
            return true;
        }

        return false;
    }

    private function call_view($view = "") {
        if (file_exists(MODULE_REPOSITORY . '/' . $this->modele->name . '/' . $this->modele->php_dir . '/' . $view . ".php")):
            $module_current = $this;
            include MODULE_REPOSITORY . '/' . $this->modele->name . '/' . $this->modele->php_dir . '/' . $view . ".php";
            print '<script text="text/javascript">';
            foreach ($this->ajax_actions as $ajax) {
                print $ajax->generate();
            }
            print '</script>';
        else:
            echo "<p>Fichier '" . MODULE_REPOSITORY . '/' . $this->modele->name . '/' . $this->modele->php_dir . '/' . $view . ".php" . "' introuvable !</p>";
        endif;
    }

    private function call_action($action = "", $ajax = 0) {
        if ($ajax == 1):
            print '<script type="text/javascript">alert("ajax activated !");</script>';
        endif;

        if (file_exists(MODULE_REPOSITORY . '/' . $this->modele->name . '/actions/' . $action . ".php")):
            $module_current = $this;
            include MODULE_REPOSITORY . '/' . $this->modele->name . '/actions/' . $action . ".php";

//$this->redirect();
        else:
            echo "<p>Fichier '" . MODULE_REPOSITORY . '/' . $this->modele->name . '/actions/' . $action . ".php" . "' introuvable !</p>";
        endif;
    }

    public function redirect($view = "") {
        if ($view != ""):
            header('Location: ' . $this->url_view($view));
        endif;
        header('Location: ' . $this->url_view());
    }

    public function getName() {
        return $this->modele->name;
    }

    public function getDisplayName() {
        return $this->modele->display_name;
    }

    public function url_action($action = "", $a = 0) {
        return current_url() . '?mn=' . $this->modele->name . '&amp;id=' . 0 . '&amp;a=' . $action . ($a == 1 ? '&amp;aj=1' : '');
    }

    public function action_ajax($id, $action, $success_funct, $title, $class = "") {
        $id = $this->modele->name . '_aj_' . $id . '_ax_' . count($this->ajax_actions);
        $this->ajax_actions[count($this->ajax_actions)] = new Module_Ajax_Action($id, $this, $action . '_action', $success_funct);

        return '<span id="' . $id . '" onclick="javascript:' . $id . '(); return false;"' . '>' . $title . '</span>';
    }

    public function url_view($view = "") {
        if ($view != ""):
            return current_url() . '?mn=' . $this->modele->name . '&amp;id=' . 0 . '&amp;v=' . $view;
        endif;
        return current_url() . '?mn=' . $this->modele->name . '&amp;id=' . 0 . '&amp;v=' . $this->view_path;
    }

    public function anchor($view, $title = "") {
        return '<a href="' . current_url() . '?mn=' . $this->modele->name . '&amp;id=' . 0 . '&amp;v=' . $view . '">' . $title . '</a>';
    }

    public function show() {
        $this->CI->load->helper('file');
        if ($this->modele->php_dir != ""):
            $install_order = false;
            $action = "";
            $ajax = 0;
            $this->view_path = "index";
            if (isset($_GET['mn'])):
                if ($_GET['mn'] == $this->modele->name):
                    if (isset($_GET['id'])):
                        if ($_GET['id'] == 0):
                            if (isset($_GET['v'])) :
                                $this->view_path = $_GET['v'];
                                if ($this->view_path == 'install'):
                                    $install_order = true;
                                endif;
                            elseif (isset($_GET['a'])):
                                $action = $_GET['a'];
                            endif;
                        endif;
                    endif;
                endif;
            endif;

            if ($action != ""):
                $this->call_action($action . '_action');
            else:
                echo '<div class="module">';

                if (!$install_order):
                    $this->call_view($this->view_path);
                else:
                    echo '<p><b>Module installed ...</b></p>';
                endif;

                echo '</div>';
            endif;
        endif;
    }
}

?>
